<?php
namespace Biz\model\theParagraph;

use Doraemon\pockets\datebase\BaseDbModel;

class CategoryClassify extends BaseDbModel
{
	const _CONFIG_FILE_ = 'db_shop';

	const _CONFIG_SELECT_ = 'db_the_paragraph';

	const _DATABASE_ = 'the_paragraph';

	const _TABLE_NAME = 'shop_category_classify';

	const STATUS_NORMAL = 1;
	const STATUS_DEL = 9;

	public static function getAllByParentId($parentId)
	{
		return self::select('*', [
			'parent_id' => $parentId,
			'status' => self::STATUS_NORMAL,
			'ORDER' => [
				'weight' => 'DESC'
			]
		]);
	}

	public static function updateById($data, $id)
	{
		$data['edittime'] = date('Y-m-d H:i:s');
		return self::update($data,['id' => $id])->rowCount();
	}

	public static function insertDate($data)
	{
		self::insert($data);
		return self::id();
	}

	public static function saveCateIds($secondClassifyId, $classifyName, $cateIds, $adminId)
	{
		self::updateById([
			'classify_name' => $classifyName,
			'admin_id' => $adminId
		], $secondClassifyId);
		$cateList = self::select_master('*', [
			'parent_id' => $secondClassifyId,
		]);
		$cateList = array_column($cateList, null, 'cate_id');
		foreach ($cateIds as $cateId) {
			if (isset($cateList[$cateId])) {
				self::updateById([
					'admin_id' => $adminId,
					'status' => self::STATUS_NORMAL
				], $cateList[$cateId]['id']);
			} else {
				self::insertDate([
					'admin_id' => $adminId,
					'parent_id' => $secondClassifyId,
					'cate_id' => $cateId,
					'status' => self::STATUS_NORMAL,
					'addtime' => date('Y-m-d H:i:s'),
					'edittime' => date('Y-m-d H:i:s')
				]);
			}
		}
		self::update(['status' => self::STATUS_DEL], [
			'parent_id' => $secondClassifyId,
			'cate_id[!]' => $cateIds
		]);
	}

	public static function getClassifyById($id)
	{
		return self::get('*', [
			'id' => $id,
			'status' => self::STATUS_NORMAL
		]);
	}

    public static function getBasicInfoByAdmin($id, $field = 'classify_name')
    {
        return self::get($field, ['id' => $id, 'status' => self::STATUS_NORMAL]);
    }
}